<?php

/**
 * 
 */
use Doctrine\ORM\Query\Expr\Select;
class Safety_Form_Role extends Zend_Validate_Abstract {
	/**
	 * @var Zend_Log
	 */	
	protected $_logger = false;
	const NAME	 = 'name';
	const DESCRIPTION	 = 'description';
	const INVALID_DATA = 'invalidData';
	
    /**
     * @var array
     */
	protected $_messageTemplates = array(
		self::NAME => "",
		self::DESCRIPTION => "",
		self::INVALID_DATA => "Invalid Data"
	);
	
	public function __construct() {
		$this->_logger = Zend_Registry::get('Zend_Log');
	}
	
	/** 
	 * Valida los campos del formulario
	 * @return boolean
	 * @author David Quispert Lazarte.
	 */
	
	public function isValid($options) {
		if (!is_array($options)) {//
            $this->_error(self::INVALID_DATA);
            return false;
        }
        // filtering data
		$formData = $this->filterFields($options);
				
		// validation data
		$validateStringLength = new Zend_Validate_StringLength(3,100);
		if ( !$validateStringLength->isValid($formData[self::NAME]) ) {		
			$this->_messageTemplates[self::NAME] = "The name, " . implode(".",$validateStringLength->getMessages());			
			$this->_error(self::NAME);			
		}
		
		// if there messages then false
		if (count($this->_messages)) {
            return false;
        } else {
            return true;
        }
	}
	
	/**
	 * Filtering data
	 * @param array $options
	 * @return array returns the filtering data	 
	 */
	public function filterFields($options) {
		// filters available
		$filterChain = new Zend_Filter();
		$filterChain->addFilter(new Zend_Filter_StripTags());
		
		$options[self::NAME] = $filterChain->filter($options[self::NAME]);
		$options[self::DESCRIPTION] = $filterChain->filter($options[self::DESCRIPTION]);
		   
		return $options;
	} 
	
}